﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCMS.Model;

namespace SCMS.CoreBusinessLogic._Inventory
{
    public class InventoryService : IInventoryService
    {
        private GeneralService generalObj;
        private _Item.ItemService itemObj;
        private _GoodsReceivedNote.GoodsReceivedNoteService grnObj;

        public InventoryService()
        {
            generalObj = new GeneralService();
            itemObj = new _Item.ItemService();
        }

        public List<Model.Inventory> GetInventoryList(Guid cpId, Guid? itemId = null, string categoryCode = null)
        {
            using (var dbContext = new SCMSEntities())
            {
                List<Model.Inventory> InList;
                if (itemId != null & categoryCode != null)
                    InList = dbContext.Inventories.Where(p => p.Item.ItemCategory.CategoryCode == categoryCode & p.ItemId == itemId & p.CountryProgrammeId == cpId).ToList();
                else if (itemId != null)
                    InList = dbContext.Inventories.Where(p => p.ItemId == itemId & p.CountryProgrammeId == cpId).ToList();
                else if (categoryCode != null)
                    InList = dbContext.Inventories.Where(p => p.Item.ItemCategory.CategoryCode == categoryCode & p.CountryProgrammeId == cpId).ToList();
                else
                    InList = dbContext.Inventories.Where(p => p.CountryProgrammeId == cpId).ToList();
                foreach (Inventory item in InList)
                {
                    Model.Item it = item.Item;
                    Model.UnitOfMeasure u = it.UnitOfMeasure;
                    Model.ItemCategory itc = it.ItemCategory;
                    Model.ItemClassification itk = it.ItemClassification;
                    Model.WareHouse wh = item.WareHouse;
                }
                return InList;
            }
        }

        public long GetItemExistingTotalQty(Guid itemId)
        {
            using (var db = new SCMSEntities())
            {
                return db.Inventories.Where(p => p.ItemId == itemId).Sum(p => p.Quantity);
            }
        }

        public List<Model.Asset> GetAssetInventoryList(Guid cpId, Guid? AssetId = null, Guid? ItemId = null)
        {
            using (var dbContext = new SCMSEntities())
            {
                List<Model.Asset> astList;
                if (ItemId != null) astList = dbContext.Assets.Where(p => p.ItemId == ItemId & p.CountryProgramId == cpId).ToList();
                else
                {
                    if (AssetId == null) astList = dbContext.Assets.Where(p => p.CountryProgramId == cpId).ToList();
                    else astList = dbContext.Assets.Where(p => p.Id == AssetId & p.CountryProgramId == cpId).ToList();
                }
                foreach (Asset item in astList)
                {
                    Model.Item it = item.Item;
                    Model.ItemClassification itk = it.ItemClassification;
                    Model.Project pro = item.ProjectDonor.Project;
                    Model.Donor donor = item.ProjectDonor.Donor;
                    Model.WareHouse wh = item.WareHouse;
                    Model.Staff s = item.Staff;
                    Model.Currency c = item.Currency;
                    if (s != null) { Model.Person p = s.Person; }
                }
                return astList;
            }
        }

        public bool IsAssetEdited(Model.Asset entity)
        {
            using (var db = new SCMSEntities())
            {
                if (entity.IsFleet)
                    if (db.FleetDetails.FirstOrDefault(p => p.AssetId == entity.Id) == null)
                        db.FleetDetails.AddObject(new FleetDetail() { Id = Guid.NewGuid(), CountryProgrammeId = entity.CountryProgramId, AssetId = entity.Id });
                db.Assets.Attach(entity);
                db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
                return (db.SaveChanges() > 0) ? true : false;
            }
        }

        public List<Model.Asset> GetAssetList(Guid cpId)
        {
            using (var db = new SCMSEntities())
            {
                return db.Assets.Where(p => p.CountryProgramId == cpId).OrderBy(a => a.Name).ToList();
            }
        }

        public List<Model.Asset> GetAssetList(Guid cpId, bool isFleet, bool returnUnregisteredFleetAsset)
        {
            using (var db = new SCMSEntities())
            {
                if (!returnUnregisteredFleetAsset)
                    return db.Assets.Where(p => p.CountryProgramId == cpId && p.IsFleet == isFleet).OrderBy(a => a.Name).ToList();

                var query = from asst in db.Assets
                            where asst.CountryProgramId == cpId && asst.IsFleet == true &&
                            !(from flit in db.FleetDetails
                              select flit.AssetId).Contains(asst.Id)
                            select asst;

                return query.ToList<Model.Asset>();
            }
        }

        public List<Model.Asset> GetAssetList(Guid cpId, string assetName)
        {
            using (var db = new SCMSEntities())
            {
                return db.Assets.Where(p => p.CountryProgramId == cpId && p.Name.ToLower().Equals(assetName.ToLower())).OrderBy(a => a.Name).ToList();
            }
        }

        public List<Depreciation> GetAnnualDepreciation(Guid assetId)
        {
            using (var db = new SCMSEntities())
            {
                var depList = db.Depreciations.Where(p => p.Period % 12 == 0 && p.AssetId == assetId).OrderBy(p => p.Period).ToList();
                foreach (var dep in depList)
                {
                    var curr = dep.Asset.Currency;
                }
                return depList;
            }
        }

        public List<Depreciation> GetDetailedDepreciation(Guid annualYearId)
        {
            using (var db = new SCMSEntities())
            {
                Model.Depreciation deprentity = db.Depreciations.FirstOrDefault(p => p.Id == annualYearId);
                DateTime statdate = deprentity.Date.AddMonths(-11);
                return db.Depreciations.Where(p => p.Date >= statdate && p.Date <= deprentity.Date && p.AssetId == deprentity.AssetId).OrderBy(p => p.Period).ToList();
            }
        }
    }

}
